<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input v-model="listQuery.keywords" placeholder="请输入搜索内容" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter" />
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">搜索</el-button>
    </div>

    <!--表格开始-->
    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%;"
    >
      <el-table-column label="ID" width="80px" align="center">
        <template slot-scope="scope">
          <el-tag>{{ scope.row.id }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="状态" width="100px" align="center">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status === 1" type="success">正常</el-tag>
          <el-tag v-if="scope.row.status === -1" type="danger">冻结</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="时间" width="250px" align="center">
        <template slot-scope="scope">
          <el-tag>{{ renderTime(scope.row.createdAt) }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="用户" width="150px" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.username.name }}</span>
        </template>
      </el-table-column>
      <el-table-column label="标题" min-width="100px" align="center">
        <template slot-scope="scope">
          <el-tag type="success">{{ scope.row.title }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="内容" min-width="100px" align="center">
        <template slot-scope="scope">
          <el-tag type="success">{{ scope.row.desc }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" min-width="100px" align="center">
        <template slot-scope="scope">
          <el-button type="primary" size="mini" @click="visibleInfo(scope.row)">详情</el-button>
          <el-button type="primary" size="mini" @click="setting(scope.row)">设置</el-button>
          <el-button type="danger" size="mini" @click="del(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!--表格结束-->
    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getPost" />
    <el-dialog
      title="设置"
      :visible.sync="edit"
      width="30%"
      height="30%"
      :before-close="handleClose"
    >
      <div style="margin-left: 90px;">
        <el-button v-if="listOne.ifTop === -1" type="success" @click="isTop('置顶',1)">设为置顶</el-button>
        <el-button v-if="listOne.ifTop === 1" type="danger" @click="isTop('置顶',1)">取消置顶</el-button>
        <el-button v-if="listOne.isFine === -1" type="success" style="margin: 0 20px;" @click="isTop('精华',2)">设为精华</el-button>
        <el-button v-if="listOne.isFine === 1" type="danger" style="margin: 0 20px;" @click="isTop('精华',2)">取消精华</el-button>
        <el-button v-if="listOne.ifBanner === -1" type="success" @click="isTop('首页横幅',3)">设为横幅</el-button>
        <el-button v-if="listOne.ifBanner === 1" type="danger" @click="isTop('首页横幅',3)">取消横幅</el-button>
      </div>
    </el-dialog>
    <el-dialog
      title="详情"
      :visible.sync="visible"
      width="40%"
      :before-close="visibleClose"
    >
      <h2>{{ info.title }}</h2>
      <p>{{ info.desc }}</p>
      <video v-if="info.video" :src="info.video" controls width="60%" style="display: block;margin: auto;" />
      <el-carousel v-if="imgList.length">
        <el-carousel-item v-for="item in imgList" :key="item" style="text-align: center;">
          <img :src="item" style="height: 100%;">
        </el-carousel-item>
      </el-carousel>
      <span slot="footer" class="dialog-footer">
        <el-button @click="visible = false">取 消</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import waves from '@/directive/waves'
import Pagination from '@/components/Pagination'
import { getPost, freeze, deletes } from '@/api/post'
import { renderTime } from '../../utils'
export default {
  name: 'List',
  components: { Pagination },
  directives: { waves },
  data() {
    return {
      tableKey: 0,
      list: null,
      total: 0,
      collection: '',
      listLoading: false,
      listQuery: {
        page: 1,
        limit: 10,
        keywords: undefined
      },
      edit: false,
      visible: false,
      imgList: [],
      id: undefined,
      listOne: {},
      info: {}
    }
  },
  created() {
    this.getPost()
  },
  methods: {
    renderTime,
    // 获取数据
    getPost() {
      this.listLoading = true
      getPost(this.listQuery).then(res => {
        const { data: { count, rows }} = res.data
        this.total = count
        this.list = rows
        setTimeout(() => {
          this.listLoading = false
        }, 500)
      }).catch(err => {
        console.log(err)
      })
    },
    // 分页
    handleFilter() {
      this.listQuery.page = 1
      this.getPost()
    },
    // 置顶
    isTop(msg, type) {
      this.$confirm('此操作将' + msg + '帖子, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        freeze(this.id, type)
          .then(res => {
            const { msg } = res.data
            this.$message({
              type: 'success',
              message: msg
            })
            this.edit = false
            this.listOne = {}
            this.getPost()
          })
          .catch(err => {
            console.log(err)
          })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消'
        })
      })
    },
    // 删除
    del(id) {
      this.$confirm('此操作将删除该帖子, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deletes(id)
          .then(res => {
            const { msg } = res.data
            this.$message({
              type: 'success',
              message: msg
            })
            this.getPost()
          })
          .catch(err => {
            console.log(err)
          })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消'
        })
      })
    },
    handleClose() {
      this.edit = false
    },
    visibleClose() {
      this.visible = false
    },
    setting(row) {
      this.listOne = row
      this.edit = true
      this.id = row.id
    },
    visibleInfo(row) {
      this.info = row
      this.imgList = row.images
      this.visible = true
    }
  }
}
</script>
<style>
  .title {
    font-weight: bold;
    font-size: 20px;
  }
</style>
